#博客首页测试用例
from selenium.webdriver.common.by import By

from common.Until import BlogDriver
from bs4 import BeautifulSoup
import requests
# import pandas as pd
class BlogList:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://127.0.0.1:8080/blog_list.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

    #测试首页（登录状态下）
    def LoginListTest(self):
        #检查博客标题是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > div.title")
        #检查博客内容是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > div.title")
        #检查按钮是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > a")

        #个人信息 -- 检查昵称是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3")

        #检查博客数量是否为0
        text = self.driver.find_element(By.CSS_SELECTOR,"#article-count").text
        print(text)
        # 断言 -- 如果元素不为0，即测试通过
        assert text != "0"
        #添加屏幕截图
        BlogDriver.getScreenShot()


    # 测试首页（未登录状态下）
    def NotLoginListTest(self):
        self.driver.back()
        self.driver.find_element(By.CSS_SELECTOR,"#userName").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        #如果在未登录的情况下访问博客列表中的博客标题会出现报错，访问不到该元素
        self.driver.find_element("body > div.container > div.right > div:nth-child(1) > div.title")

    #登录状态下退出账号
    def LoginExitTest(self):
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(6)").click()
        # 获取当前页面的句柄
        curHandle = self.driver.current_window_handle
        # 获取素有元素的句柄
        allHandLe = self.driver.window_handles
        # 遍历所有元素的句柄，切换到新的页面
        for handle in allHandLe:
            if handle != curHandle:
                # 执行切换句柄
                self.driver.switch_to.window(handle)

        title = self.driver.title
        url = self.driver.current_url
        print(title)
        print(url)

        username = self.driver.find_element(By.CSS_SELECTOR,"#userName").text
        password = self.driver.find_element(By.CSS_SELECTOR,"#password").text
        #断言一下  登录页的账号和密码输入框为空 -- 测试通过
        assert username == ""
        assert password == ""

# list = BlogList()
# list.LoginListTest()



